package com.overlord.im.commons.message;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageCodec;

import java.util.List;

/**
 * @ClassName Byte2MessageEndocerHandler
 * @Author wuwei
 * @Description NONE
 * @Date 2020/5/9 11:45
 **/
public class Byte2MessageCodec extends ByteToMessageCodec<OverlordImMessage> {

    private Protocol protocol;

    public Byte2MessageCodec(Protocol protocol) {
        this.protocol = protocol;
    }

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, OverlordImMessage o, ByteBuf byteBuf)
            throws Exception {
        protocol.encode(byteBuf, o);
    }

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) throws Exception {
        OverlordImMessage decode = protocol.decode(byteBuf);
        list.add(decode);
    }
}
